package com.softlab.logsystem.common.exception;

import com.softlab.logsystem.common.RestData;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author LiXiwen
 * @date 2019/7/18 20:23
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(LogException.class)
    public RestData LogException(final Exception ex) {
        LogException e = (LogException) ex;
        logger.warn("LogException : " + e.getLocalizedMessage());
        return new RestData(1, e.getLocalizedMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public RestData runtimeLogException(HttpServletRequest request, final Exception ex,
                                        HttpServletResponse response) {
        RuntimeException e = (RuntimeException) ex;
        logger.warn("runtimeLogException : " + e.getLocalizedMessage());
        return new RestData(1, e.getLocalizedMessage());
    }
}
